package com.lzq.model.visitor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/29 14:50
 * @updateUser : modify
 * @updateTime : 2022/11/29 14:50
 * @updateRemark : 访问者模式
 */
public class Test {
    public static void main(String[] args) {
        Home home = new Home();
        home.add(new Cat());
        home.add(new Dog());
        home.action(new Owner());
    }
}

interface Person {
    /**
     * 喂猫
     *
     * @param cat
     */
    void feed(Cat cat);

    /**
     * 喂狗
     *
     * @param dog
     */
    void feed(Dog dog);
}

class Owner implements Person {
    @Override
    public void feed(Cat cat) {
        System.out.println("主人 喂猫");
    }

    @Override
    public void feed(Dog dog) {
        System.out.println("主人 喂狗");
    }
}

class Someone implements Person {
    @Override
    public void feed(Cat cat) {
        System.out.println("其他人 喂猫");
    }

    @Override
    public void feed(Dog dog) {
        System.out.println("其他人 喂狗");
    }
}

interface Animal {
    /**
     * 实现方法
     *
     * @param person
     */
    void accept(Person person);
}

class Cat implements Animal {
    @Override
    public void accept(Person person) {
        person.feed(this);
        System.out.println("喵喵喵.....");
    }
}

class Dog implements Animal {
    @Override
    public void accept(Person person) {
        person.feed(this);
        System.out.println("汪汪汪.....");
    }
}

class Home {
    private List<Animal> nodeList = new ArrayList<Animal>();

    public void action(Person person) {
        for (Animal node : nodeList) {
            node.accept(person);
        }
    }

    /**
     * 添加操作
     *
     * @param animal
     */
    public void add(Animal animal) {
        nodeList.add(animal);
    }
}